package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.utils.TraceUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.*;
import com.tbit.main.exception.BaseException;
import com.tbit.main.h2.pojo.ParkPoint;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.BleBeacon;
import com.tbit.main.pojo.Parameter;
import com.tbit.main.pojo.ParkPointBoundAbnormal;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * 道钉
 *
 * @author LMY
 * @create 2020-11-16 17:21
 */
@RestController
@RequestMapping("/bleBeacon")
public class BleBeaconController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private BleBeaconService bleBeaconService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private ParkPointBoundAbnormalService parkPointBoundAbnormalService;
    @Autowired
    private ParkPointService parkPointService;

    /**
     * 添加
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.BLEBEACON, methods = MethodEnum.BLEBEACON_ADD)
    public JsonResult add(String token, Integer accountId, BleBeacon bleBeacon) {

        Assert.notNull(bleBeacon.getParkPointId(), "ParkPoint.id.notNull");
        Assert.notNull(bleBeacon.getMachineNO(), "Machine.userCode.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        BleBeacon bleBeaconDB = bleBeaconService.getByMachineNO(bleBeacon.getMachineNO());
        if (bleBeaconDB != null) {
            return JsonResult.fail(500, "该设备已绑定站点", TraceUtil.getTraceIdByLocal());
        }

        bleBeacon.setAccountId(accountId);
        bleBeaconService.insert(bleBeacon);
        return JsonResult.succ();
    }

    /**
     * 查询站点道钉
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String token, Integer accountId, Integer state, Integer parkPointId,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                @RequestParam(defaultValue = "10") Integer rowCount) {
        Assert.notNull(parkPointId, "ParkPoint.id.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<BleBeacon> list = bleBeaconService.queryPage(state, parkPointId, pageNO, rowCount);
        Integer total = bleBeaconService.queryCount(state, parkPointId);
        return JsonResult.succ(total, list);
    }

    /**
     * 批量添加
     */
    @RequestMapping("/addBatch")
    @OperateLog(module = ModuleEnum.BLEBEACON, methods = MethodEnum.BLEBEACON_ADD)
    public JsonResult addBatch(String token, Integer accountId, Integer parkPointId, String machineNOs) {

        Assert.notNull(parkPointId, "AccountUser.agentId.notNull");
        Assert.notNull(machineNOs, "Machine.userCode.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        List<String> list = Arrays.asList(machineNOs.split(","));
        List<String> exist = bleBeaconService.addBatch(parkPointId, list, accountId);
        return JsonResult.succ(exist);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @OperateLog(module = ModuleEnum.BLEBEACON, methods = MethodEnum.BLEBEACON_DEL)
    public JsonResult delete(String token, Integer accountId, Integer bleBeaconId) {

        Assert.notNull(bleBeaconId, "BleBeacon.control.bleBeaconId.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        bleBeaconService.delete(bleBeaconId);
        return JsonResult.succ();
    }

    /**
     * 查询运营区域所有道钉
     */
    @RequestMapping("getBleBeacon")
    public JsonResult getBleBeacon(String token, Integer accountId, Integer mapType,
                                   @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        AccountUser accountUser = tokenService.getAccount(token);
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUser.getAccountUserId());
        }

        List<ParkPoint> parkPoints = new LinkedList<>();
        if (CollUtil.isEmpty(regionId)) {
            parkPoints = bleBeaconService.getBleBeacon(accountId);
        } else {
            parkPoints = bleBeaconService.getBleBeaconRegionId(regionId);
        }

        /** 校准 */
        calibrateService.celibrateParkPoint(parkPoints, mapType);

        return JsonResult.succ(parkPoints);
    }

    /**
     * 更新设备时间
     *
     * @param token
     * @param parameter
     * @return
     */
    @RequestMapping("/updateTime")
    public JsonResult updateTime(String token, Parameter parameter) {
        bleBeaconService.updateTime(parameter.getMachineNOs());
        return JsonResult.succ();
    }


    /**
     * 修改设备
     *
     * @param token
     * @return
     */
    @RequestMapping("/abnormal")
    @OperateLog(module = ModuleEnum.BLEBEACON, methods = MethodEnum.BLEBEACON_UPDATE)
    public JsonResult abnormal(String token, String machineNO, Integer state) {

        Assert.notNull(machineNO, "Machine.machineNO.notNull");

        BleBeacon bleBeacon = bleBeaconService.getByMachineNO(machineNO);
        if (bleBeacon == null) {
            throw new BaseException("machienNO.is.notNull");
        }

        if (state != null) {
            /**修改异常状态*/
            bleBeaconService.update(machineNO, state, null);
            if (state.equals(ParkPointBoundConstant.state_normal)) {
                /**删除异常*/
                parkPointBoundAbnormalService.del(machineNO);
            } else {
                /**添加异常*/
                BleBeacon bleBeaconDb = bleBeaconService.getByMachineNO(machineNO);
                ParkPoint parkPoint = parkPointService.getByParkPointId(bleBeaconDb.getParkPointId());
                AccountUser accountUserToken = tokenService.getAccount(token);
                ParkPointBoundAbnormal parkPointBoundAbnormal = new ParkPointBoundAbnormal();
                parkPointBoundAbnormal.setMachineNO(machineNO);
                parkPointBoundAbnormal.setState(state);
                parkPointBoundAbnormal.setType(ParkPointBoundConstant.type_ble);
                parkPointBoundAbnormal.setAddTime(DateTimeUtil.getNowTime());
                parkPointBoundAbnormal.setUserMainId(accountUserToken.getAccountUserId());
                parkPointBoundAbnormal.setAccountId(parkPoint.getAccountId());
                parkPointBoundAbnormalService.add(parkPointBoundAbnormal);
            }
        } else {
            /**删除绑定*/
            bleBeaconService.delNO(machineNO);
            parkPointBoundAbnormalService.del(machineNO);
        }
        return JsonResult.succ();
    }

    /**
     * 修改设备备注
     *
     * @param token
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.BLEBEACON, methods = MethodEnum.BLEBEACON_UPDATE)
    public JsonResult update(String token, String machineNO, String remark) {

        Assert.notNull(machineNO, "Machine.machineNO.notNull");

        BleBeacon bleBeaconDb = bleBeaconService.getByMachineNO(machineNO);
        if (bleBeaconDb == null) {
            throw new BaseException("machienNO.is.notNull");
        }

        /**修改异常状态*/
        bleBeaconService.update(machineNO, null, remark);
        return JsonResult.succ();
    }

}
